//
//  HTClassStorage.swift
//  QuitSmokingApp
//
//  Created by   on 2024/3/6.
//

import Foundation
import DefaultsKit


class HTClassStorage {
    static let var_defaults = Defaults()
    
    static func ht_setNavBg(var_type: String) {
        var_defaults.set(var_type, for: .var_defaultNavBg)
    }
    
    static func ht_getNavBg() -> String {
        return var_defaults.get(for: .var_defaultNavBg) ?? "0"
    }
    
    /// 保存今日计划
    /// - Parameter var_model:
    static func ht_SaveTodayPlan(var_model: SmokingCessationPlanModel) {
        /// 戒烟计划的Key
        var var_dict = var_defaults.get(for: .var_smokingCessationPlan) ?? [:]
        let var_dict_key = HTClassTool.ht_obtainYearMonthDay()
        var_dict[var_dict_key] = var_model
        var_defaults.set(var_dict, for: .var_smokingCessationPlan)
    }
    
    /// 获取全部的戒烟计划
    /// - Returns:
    static func ht_getAllPlan() -> [String: SmokingCessationPlanModel] {
        return var_defaults.get(for: .var_smokingCessationPlan) ?? [:]
    }
    
    /// 获取今日计划
    /// - Returns:
    static func ht_getToDayPlan() -> SmokingCessationPlanModel? {
        let var_dict = var_defaults.get(for: .var_smokingCessationPlan) ?? [:]
        let var_dict_key = HTClassTool.ht_obtainYearMonthDay()
        return var_dict[var_dict_key]
    }
    
    /// 签到
    /// - Parameter var_signinDate:签到日期
    static func ht_signin(var_signinDate: String) {
        /// 已签到list
        var var_arr = ht_getSignedinList()
        var_arr.append(var_signinDate)
        var_defaults.set(var_arr, for: .var_signedinList)
    }
    
    /// 获取已签到list
    /// - Returns:已签到list
    static func ht_getSignedinList() -> [String] {
        /// 已签到list
        return var_defaults.get(for: .var_signedinList) ?? []
    }
    
    /// 新增积分
    /// - Parameter var_integral:积分数
    static func ht_addIntegral(var_integral: Int) {
        /// 存储的积分
        var var_totalIntegral = ht_getAllIntegral()
        var_totalIntegral += var_integral
        var_defaults.set(var_totalIntegral, for: .var_integral)
    }
    
    /// 使用积分
    /// - Parameter var_integral:积分数
    static func ht_useIntegral(var_integral: Int) {
        /// 存储的积分
        var var_totalIntegral = ht_getAllIntegral()
        var_totalIntegral -= var_integral
        var_defaults.set(var_totalIntegral, for: .var_integral)
    }
    
    /// 获取所有积分
    /// - Returns:所有积分
    static func ht_getAllIntegral() -> Int {
        /// 已签到list
        return var_defaults.get(for: .var_integral) ?? 0
    }
    
    /// 获取已拥有button
    /// - Returns:所有已拥有button
    static func ht_getHaveButtonList() -> [Int] {
        /// 已拥有buttonlist
        return var_defaults.get(for: .var_haveButtonList) ?? [0]
    }
    
    /// 新增已拥有button
    /// - Parameter:新增button
    static func ht_addHaveButton(var_button: Int) {
        var var_haveList = ht_getHaveButtonList()
        var_haveList.append(var_button)
        /// 已拥有buttonlist
        var_defaults.set(var_haveList, for: .var_haveButtonList)
    }
    
    /// 获取当前使用button
    /// - Returns:当前使用button
    static func ht_getCurrentButton() -> Int {
        /// 当前使用button
        return var_defaults.get(for: .var_currentButton) ?? 0
    }
    
    /// 更新当前使用button
    /// - Parameter:当前使用button
    static func ht_updateCurrentButton(var_currentButton: Int) {
        /// 当前使用button
        var_defaults.set(var_currentButton, for: .var_currentButton)
    }
    
    
    static func ht_saveHeaderImage(_ var_imgae: UIImage?) {
        if let var_img = var_imgae, let var_data = var_img.pngData() {
//            var_defaults.set(var_data, for: .var_headerImage)
            UserDefaults.standard.set(var_data, forKey: "string_headerImage_data")
            UserDefaults.standard.synchronize()
        }
    }
    
    static func ht_getHeaderImage() -> UIImage? {
        if let var_data = UserDefaults.standard.data(forKey: "string_headerImage_data"), let var_image = UIImage(data: var_data) {
            return var_image
        }
        return nil
    }
    
    
    static func ht_savePersonalInfor(_ var_info: HTClassPersonalInforModel) {
        var_defaults.set(var_info, for: .var_personalInfor)
    }
    
    static func ht_getPersonalInfor() -> HTClassPersonalInforModel? {
        return var_defaults.get(for: .var_personalInfor)
    }
    
    static func ht_saveLogin(_ var_login: Bool) {
        var_defaults.set(var_login, for: .var_login)
    }
    
    static func ht_getLogin() -> Bool {
        return var_defaults.get(for: .var_login) ?? false
    }
    
    static func ht_savePalnShow(_ var_palnShow: Bool) {
        var_defaults.set(var_palnShow, for: .var_pop_plan_show)
    }
    
    static func ht_getPalnShow() -> Bool {
        return var_defaults.get(for: .var_pop_plan_show) ?? false
    }
    
    //保存内购信息
    static func ht_savePurchaseInfo(_ var_info: HTClassPurchaseInfoModel) {
        var_defaults.set(var_info, for: .var_purchaseInfo)
    }
    
    //获取内购信息
    static func ht_getPurchaseInfo() -> HTClassPurchaseInfoModel? {
        return var_defaults.get(for: .var_purchaseInfo) ?? HTClassPurchaseInfoModel()
    }
}

